<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :destroyOnClose="true"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="功能名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'name', validatorRules.name]" placeholder="请输入功能名称" ></a-input>
        </a-form-item>
        <a-form-item label="功能编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'code', validatorRules.code]" placeholder="请输入功能编号" ></a-input>
        </a-form-item>
        <a-form-item label="功能编号" :labelCol="labelCol" :wrapperCol="wrapperCol" hidden>
          <a-input v-decorator="[ 'pid', {}]" ></a-input>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { addDeviceFunc, editDeviceFunc, funcCheckCode } from '../service.js'
  import pick from 'lodash.pick'
  import JSelectPosition from '@/components/jeecgbiz/JSelectPosition'

  export default {
    name: 'DeviceFunctionChildModal',
    components: {
      JSelectPosition
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width: 800,
        visible: false,
        readOnly: true,
        model: {},
        pid: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },

        confirmLoading: false,
        validatorRules: {
          name: { rules: [{ required: true, message: '请输入功能名称' }] },
          code: { rules: [{ required: true, message: '请输入功能编号' },
                          { validator: this.validateMyCode }
                          ] },
          pid: {}
        },
        url: {

        },
        expandedRowKeys: [],
        subExpandedKeys: [],
        pidField: 'pid'
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        console.log(this.model);
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'pid', 'name', 'code'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let formData = Object.assign(this.model, values);
            console.log('表单提交数据', formData)
            let obj
            if (!this.model.id) {
              obj = addDeviceFunc(formData)
            } else {
              obj = editDeviceFunc(formData)
            }
            obj.then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.submitSuccess(formData)
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
      submitSuccess(formData) {
        if (!formData.id) {
          this.expandedRowKeys = []
          if (formData.pid && this.expandedRowKeys.length === 0) {
            this.expandedRowKeys = this.subExpandedKeys;
          }
          this.$emit('ok', formData, this.expandedRowKeys.reverse());
        } else {
          this.$emit('ok', formData);
        }
      },
      validateMyCode(rule, value, callback) {
        let params = {
          pid: this.form.getFieldValue('pid'),
          code: value
        }
        funcCheckCode(params).then((res) => {
          if (res.success) {
            callback()
          } else {
            callback(res.message)
          }
        })
      }
    }
  }
</script>
